<?php
namespace Modules\ShopApi\Models;
use Illuminate\Support\Facades\DB;
use Modules\ShopApi\Models\AuthArea as AuthAreaModel;
use Modules\ShopApi\Services\ShopUserService;
class ShopSite extends BaseApiModel
{
	/**
	 * @name  添加地址
	 * @return json 
	 */
    public function add_address($data){
		$user_info = (new ShopUserService())->my();
		$data['province_name'] = AuthAreaModel::where('id',$data['province_id'])->value('name');
		$data['city_name'] = AuthAreaModel::where('id',$data['city_id'])->value('name');
		$data['county_name'] = AuthAreaModel::where('id',$data['county_id'])->value('name');
		$data['user_id'] = $user_info['id'];
		$data['created_at'] = date('Y-m-d H:i:s');
		DB::beginTransaction();
		try{
			if($data['status'] == 1){
				$this->where('id','>',0)->where(['user_id'=>$data['user_id'],'status'=>1])->update([
					'status'=>0
				]);
			}
			$this->insert($data);
			DB::commit();
		}catch(\Exception $e){
			DB::rollBack();
			return $this->api_error();
		}
		return $this->api_success();
	}
	/**
	 * @name  地址列表
	 * @return json 
	 */
    public function get_address_list(){
		$user_info = (new ShopUserService())->my();
		$list = $this->select('id','name','phone','province_name','city_name','county_name','detailed_address','status')->orderBy('status','desc')->orderBy('id','desc')->where(['user_id'=>$user_info['id'],'is_delete'=>0])->get()->toArray();
		return $this->api_success(['data'=>$list]);
	}
	/**
	 * @name  选择默认地址
	 * @return json 
	 */
    public function status($id){
		$user_info = (new ShopUserService())->my();
		DB::beginTransaction();
		try{
			$this->where(['id'=>$id,'user_id'=>$user_info['id']])
					->update(['status'=>1]);
			$this->where('id','!=',$id)
					->where(['user_id'=>$user_info['id'],'status'=>1])
					->update(['status'=>0]);
			DB::commit();
		}catch(\Exception $e){
			DB::rollBack();
			return $this->api_error();
		}
		return $this->api_success();
	}
	/**
	 * @name  获取编辑地址数据
	 * @return json 
	 */
    public function get_address_info($id){
		$user_info = (new ShopUserService())->my();
		$info = $this->select('id','name','phone','province_name','city_name','county_name','detailed_address','status','province_id','city_id','county_id')->where(['user_id'=>$user_info['id'],'id'=>$id])->first();
		if($info){
			$info = $info->toArray();
		}else{
			$info = [];
		}
		return $this->api_success(['data'=>$info]);
	}
	/**
	 * @name  编辑地址
	 * @return json 
	 */
    public function edit_address($data){
		$user_info = (new ShopUserService())->my();
		$data['province_name'] = AuthAreaModel::where('id',$data['province_id'])->value('name');
		$data['city_name'] = AuthAreaModel::where('id',$data['city_id'])->value('name');
		$data['county_name'] = AuthAreaModel::where('id',$data['county_id'])->value('name');
		$data['user_id'] = $user_info['id'];
		$data['created_at'] = date('Y-m-d H:i:s');
		$id = $data['id'];
		unset($data['id']);
		DB::beginTransaction();
		try{
			if($data['status'] == 1){
				$this->where('id','!=',$id)->where(['user_id'=>$data['user_id'],'status'=>1])->update([
					'status'=>0
				]);
			}
			$this->where(['id'=>$id])->update($data);
			DB::commit();
		}catch(\Exception $e){
			DB::rollBack();
			return $this->api_error();
		}
		return $this->api_success();
	}
	/**
	 * @name  删除收货地址
	 * @return json 
	 */
    public function delete_address($id){
		$user_info = (new ShopUserService())->my();
		if($this->where(['id'=>$id,'user_id'=>$user_info['id']])->update(['is_delete'=>1])){
			return $this->api_success();
		}else{
			return $this->api_error();
		};
	}
}
